import Vue from 'vue'
import VueRouter from 'vue-router'
import { Lazyload } from 'vant'

import { getToken } from '@/utils/storage'

import routes from './routes'

import '@/utils/vant'

Vue.use(VueRouter)
Vue.use(Lazyload)

const router = new VueRouter({
  routes
})

const whiteList = ['/login', '/register']
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
